<?php
/*
    常用方法 yjmp
*/

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
/*检查购物车中是否存在预约商品*/
function is_res_cart($user_id)
{
    $cart_goods_ids = $GLOBALS['db']->getAll('SELECT goods_id FROM '.$GLOBALS['ecs']->table('cart').' WHERE user_id = '.$user_id.'');
    $has_reservation = 0;
    if ($cart_goods_ids) {
        foreach ($cart_goods_ids as $k => $v) {
            $is_reservation = $GLOBALS['db']->getOne('SELECT is_reservation FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = '.$v['goods_id'].'');
            $has_reservation = $is_reservation == 1 ? 1 : 0;
        }
    }
    return $has_reservation;
}
/*预约商品购物车中的预约个数*/
function get_res_num($user_id)
{
    $num =  $GLOBALS['db']->getOne('SELECT goods_number FROM '.$GLOBALS['ecs']->table('cart').' WHERE user_id = '.$user_id);
    $resutl = array();
    if ($num) {
       for ($i=0; $i < $num; $i++) { 
            $resutl[$i] = $i;
        }
    }
    return $resutl;
}

/*判断商品是否为预约商品*/
function is_res_goods($goods_id)
{
    return $GLOBALS['db']->getOne('SELECT is_reservation FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = '.$goods_id);
}

/**
 *  获取用户指定范围的预约订单列表
 *
 * @access  public
 * @param   int         $user_id        用户ID号
 * @param   int         $num            列表最大数量
 * @param   int         $start          列表起始位置
 * @return  array       $order_list     订单列表
 */
function get_user_resorders($user_id, $num = 10, $start = 0)
{
    /* 取得订单列表 */
    $arr    = array();

    $sql = "SELECT order_id, order_sn, order_status, shipping_status, pay_status, add_time, " .
           "(goods_amount + shipping_fee + insure_fee + pay_fee + pack_fee + card_fee + tax - discount) AS total_fee ".
           " FROM " .$GLOBALS['ecs']->table('order_info') .
           " WHERE user_id = '$user_id' AND is_reservation = 1 ORDER BY add_time DESC";
    $res = $GLOBALS['db']->SelectLimit($sql, $num, $start);

    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['order_status'] == OS_UNCONFIRMED)
        {
            $row['handler'] = "<a href=\"user.php?act=cancel_order&order_id=" .$row['order_id']. "\" onclick=\"if (!confirm('".$GLOBALS['_LANG']['confirm_cancel']."')) return false;\">".$GLOBALS['_LANG']['cancel']."</a>";
        }
        else if ($row['order_status'] == OS_SPLITED)
        {
            /* 对配送状态的处理 */
            if ($row['shipping_status'] == SS_SHIPPED)
            {
                @$row['handler'] = "<a href=\"user.php?act=affirm_received&order_id=" .$row['order_id']. "\" onclick=\"if (!confirm('".$GLOBALS['_LANG']['confirm_received']."')) return false;\">".$GLOBALS['_LANG']['received']."</a>";
            }
            elseif ($row['shipping_status'] == SS_RECEIVED)
            {
                @$row['handler'] = '<span style="color:red">'.$GLOBALS['_LANG']['ss_received'] .'</span>';
            }
            else
            {
                if ($row['pay_status'] == PS_UNPAYED)
                {
                    @$row['handler'] = "<a href=\"user.php?act=order_detail&order_id=" .$row['order_id']. '">' .$GLOBALS['_LANG']['pay_money']. '</a>';
                }
                else
                {
                    @$row['handler'] = "<a href=\"user.php?act=order_detail&order_id=" .$row['order_id']. '">' .$GLOBALS['_LANG']['view_order']. '</a>';
                }

            }
        }
        else
        {
            $row['handler'] = '<span style="color:red">'.$GLOBALS['_LANG']['os'][$row['order_status']] .'</span>';
        }

        $row['shipping_status'] = ($row['shipping_status'] == SS_SHIPPED_ING) ? SS_PREPARING : $row['shipping_status'];
        $row['order_status'] = $GLOBALS['_LANG']['os'][$row['order_status']] . ',' . $GLOBALS['_LANG']['ps'][$row['pay_status']] . ',' . $GLOBALS['_LANG']['ss'][$row['shipping_status']];

        $arr[] = array('order_id'       => $row['order_id'],
                       'order_sn'       => $row['order_sn'],
                       'order_time'     => local_date($GLOBALS['_CFG']['time_format'], $row['add_time']),
                       'order_status'   => $row['order_status'],
                       'total_fee'      => price_format($row['total_fee'], false),
                       'handler'        => $row['handler']);
    }

    return $arr;
}
//购物车深度清理 
function deep_clear_cart()
{
    if ($_SESSION['user_id']) {
        $GLOBALS['db']->query(' DELETE  FROM '.$GLOBALS['ecs']->table('cart').' WHERE user_id = '.$_SESSION['user_id'].'');  
    }
}
?>